package edu.uta.cse6339.facetedinterface.classifier.src.facetedExplorationMain;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class ClassifierHelper
{
	static HashMap<Integer, String> typeIdClassMapper = null;
	
	static final int numberOfDimensions = 15;
	
	public HashMap<Integer, HashSet<Integer>> populateClassedArticles(String filePath, HashMap<String, Integer> invertedDocIdMapper)
	{
		HashMap<Integer, HashSet<Integer>> docClassList = new HashMap<Integer, HashSet<Integer>>();
		try
		{
			BufferedReader br = new BufferedReader(new FileReader(filePath));
			try
			{
				String line = br.readLine();
				if(typeIdClassMapper == null && line != null)
				{
					// Assign a numeric value to each type (which is in the first line of "filepath".. Need not do it more than once..
					typeIdClassMapper = new HashMap<Integer, String>();
					String[] classes = line.split(",");
					int len = classes.length;
					System.out.println("len = " + len + "\nnumfclasses = " + Classifier.numOfClasses);
					// the first column is "id". not a class label exactly. so leave it.
					if(len-1 != Classifier.numOfClasses)
					{
						System.out.println("ERROR: The number of classes found in the csv file is not the same as in hardcoded value");
					}
					for(int i=1; i<=Classifier.numOfClasses; i++)
					{
						typeIdClassMapper.put(i, classes[i]);
					}
				}
				
				line = null;
				while((line = br.readLine()) != null)
				{
					String[] articleClasses = line.split(",");
					// the first column is the article link (id)..
					if(invertedDocIdMapper.containsKey(articleClasses[0]))
					{
						int docId = invertedDocIdMapper.get(articleClasses[0]);
						HashSet<Integer> labelList = new HashSet<Integer>();
						int len = articleClasses.length;
						//System.out.println(len + ":" + Classifier.numOfClasses);
						for(int i=1; i<len; i++)
						{
							if(!articleClasses[i].isEmpty())
							{
								labelList.add(i);
							}
						}
						docClassList.put(docId, labelList);
					}
					else
					{
						System.out.println("Error.. Have not seen this article already.. " + articleClasses[0]);
					}
				}
			}
			finally
			{
				br.close();
			}
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
		return docClassList;
	}
}
